<template>
    <div class="container">
        <system-nav name="发布消息" :showBack="true"></system-nav>
        <div class="inner">
            <div class="system-form system-form-large">
                <Form ref="form" :model="form" :rules="ruleValidate" :label-width="120" :hide-required-mark="true">
                    <FormItem prop="title" label="标题">
                        <Input v-model="form.title" placeholder="请输入标题" /> 
                    </FormItem>
                    <FormItem prop="targetUserIds" label="接收人">
                        <member-tree v-model="form.targetUserIds" @input="onChange($event, 'targetUserIds')" />
                    </FormItem>
                    <FormItem prop="content" label="消息内容">
                        <system-quill-editor v-model="form.content" @inputfuns="onChange($event, 'content')"></system-quill-editor>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" class="margin-right-25" @click="handleSubmit('form')">发送</Button>
                        <Button type="primary" @click="goBack" ghost>返回</Button>
                    </FormItem>
                </Form>
            </div>
        </div>
    </div>
</template>

<script>
import SystemNav from '@/components/SystemNav'
import SystemQuillEditor from '@/components/SystemQuillEditor'
import MemberTree from '@/components/MemberTree'
import mixin from '@/mixins'
import { sendMessageRest } from '@/common/apis/system'

export default {
    name: "messageAdd",
    mixins: [mixin],
    components: {
        SystemNav,
        SystemQuillEditor,
        MemberTree
    },
    data() {
        return {
            type: 3,
            form: {
                title: '',
                targetUserIds: [],
                fileSHA1: '',
                content: ''
            },
            ruleValidate: {
                title: [
                    { required: true, message: '标题不能为空', trigger: 'blur' }
                ],
                targetUserIds: [
                    { required: true, type: 'array', message: '接收人不能为空', trigger: 'change' }
                ],
                content: [
                    { required: true, message: '消息内容不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {

    },
    methods: {
        onChange(val, type) {
            this.$nextTick(() => {
                this.form[type] = val
                this.$refs.form.validateField(type)
            })
        },
        handleSubmit(name) {
            this.$refs[name].validate(valid => {
                if (valid) {
                    const params = {
                        ...this.form,
                        type: this.type
                    }
                    sendMessageRest(params).then(result => {
                        if (result.data.code === 200) {
                            this.handleSuccess()
                        } else {
                            this.handleError(result.data.msg)
                        }
                    })
                }
            })
        },
        handleSuccess(msg) {
            this.goBack()
            this.$Message.success({
                background: true,
                content: msg || '发布成功'
            })
        },
        handleError(msg) {
            this.reset()
            this.$Message.error({
                background: true,
                content: msg || '发布失败'
            })
        },
        reset() {
            this.$refs.form.resetFields()
        }
    }
}
</script>
